<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package}.mapper.${className}Mapper">
  <resultMap id="BaseResultMap" type="${package}.dto.${className}Dto">
<#if columns??>
  <#list columns as column>
    <result column="${column.column}" jdbcType="${column.columnType}" property="${column.changeField}" />
  </#list>
</#if> 
  </resultMap>
  <sql id="Base_Column_List">
<#if columns??>
  <#list columns as column>
  <#if column_has_next>
  	${column.column},
  </#if>
  <#if !column_has_next>
  	${column.column}
  </#if>
  </#list>
</#if> 
  </sql>
  <sql id="Example_Where_Clause">
    <where>
<#if columns??>
  <#list columns as column>
  	<#if column.columnType != 'VARCHAR' && column.columnType != 'CHAR' && column.select = 'Y' >
  	<if test="${column.changeField} != null "> 
  	 	AND ${column.column} = ${r"#{"} ${column.changeField},jdbcType=${column.columnType} ${r"}"} 
    </if>
  	</#if>
  	<#if (column.columnType = 'VARCHAR' || column.columnType = 'CHAR') && column.select = 'Y' >
	<if test="${column.changeField} != null and ${column.changeField} != '' ">
		AND ${column.column} = ${r"#{"} ${column.changeField},jdbcType=${column.columnType} ${r"}"} 
    </if>
  	</#if>       
  </#list>
</#if> 
    </where>
  </sql>
  <!--根据${package}.dto.${className}Dto查询${tableComment}条件数据-->
  <select id="select${className}List" parameterType="${package}.dto.${className}Dto" resultMap="BaseResultMap">
     SELECT 
    <include refid="Base_Column_List" />
     FROM ${tableName} 
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
     ORDER BY id desc 
  </select>
  <!--根据${package}.dto.${className}Dto查询所有满足${tableComment}条件数据-->
  <select id="selectAll${className}List" parameterType="${package}.dto.${className}Dto" resultMap="BaseResultMap">
     SELECT 
    <include refid="Base_Column_List" />
     FROM ${tableName} 
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
     ORDER BY id desc 
  </select>
  <!-- 根据${package}.dto.${className}Dto 查询${tableComment}ID数据 -->
  <select id="select${className}ById" parameterType="${package}.dto.${className}Dto" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from ${tableName}
    where id = ${r"#{"}id,jdbcType=BIGINT${r"}"}
  </select>
  <!-- 根据${package}.dto.${className}Dto插入${tableComment}数据 -->
  <insert id="save${className}Dto" parameterType="${package}.dto.${className}Dto">
    insert into ${tableName}
    <trim prefix="(" suffix=")" suffixOverrides=",">
<#if columns??>
  <#list columns as column>
  	<#if column.columnType != 'VARCHAR' && column.columnType != 'CHAR' && column.save = 'Y'>
  	<#if column.defaultVal = '' >
  	<if test="${column.changeField} != null ">
         ${column.column} , 
    </if>
    <#else>
    ${column.column} ,
  	</#if>
  	</#if>
  	<#if (column.columnType = 'VARCHAR' || column.columnType = 'CHAR') && column.save = 'Y' >
    <#if column.defaultVal = '' >
  	<if test="${column.changeField} != null  and ${column.changeField} != '' ">
         ${column.column} , 
    </if>
    <#else>
    ${column.column} ,
  	</#if>
  	</#if>
  </#list>
</#if> 
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
<#if columns??>
  <#list columns as column>
  	<#if column.columnType != 'VARCHAR' && column.columnType != 'CHAR' && column.save = 'Y'>
    <#if column.defaultVal = '' >
  	<if test="${column.changeField} != null">
         ${r"#{"} ${column.changeField},jdbcType=${column.columnType}${r"}"}, 
    </if>
    <#else>
    ${column.defaultVal} ,
  	</#if>
  	</#if>
  	<#if (column.columnType = 'VARCHAR' || column.columnType = 'CHAR') && column.save = 'Y'>
    <#if column.defaultVal = '' >
  	<if test="${column.changeField} != null  and ${column.changeField} != '' ">
         ${r"#{"} ${column.changeField},jdbcType=${column.columnType}${r"}"}, 
    </if>
    <#else>
    ${column.defaultVal} ,
  	</#if>
  	</#if>
  </#list>
</#if> 
    </trim>
  </insert>
  <!-- 根据${package}.dto.${className}Dto更新${tableComment}数据 -->
  <update id="update${className}ById" parameterType="${package}.dto.${className}Dto">
    update ${tableName}
    <set>
<#if columns??>
  <#list columns as column>
  	<#if column.columnType != 'VARCHAR' && column.columnType != 'CHAR' && column.update = 'Y'>
    <#if (column.defaultVal = '') >
  	<if  test="${column.changeField} != null">
         ${column.column} = ${r"#{"} ${column.changeField},jdbcType=${column.columnType}${r"}"},
    </if>
    <#else>
    ${column.column} = ${column.defaultVal},
  	</#if>
  	</#if>
  	<#if (column.columnType = 'VARCHAR' || column.columnType = 'CHAR') && column.update = 'Y'>
    <#if (column.defaultVal = '') >
  	<if test="${column.changeField} != null  and ${column.changeField} != '' ">
         ${column.column} = ${r"#{"} ${column.changeField},jdbcType=${column.columnType}${r"}"},
    </if>
    <#else>
    ${column.column} = ${column.defaultVal},
  	</#if>
  	</#if>
  </#list>
</#if> 
    </set>
    where id = ${r"#{"}id,jdbcType=BIGINT${r"}"}
  </update>
  <!-- 根据${package}.dto.${className}Dto更新${tableComment}数据 -->
  <update id="cancel${className}ById" parameterType="${package}.dto.${className}Dto">
    update ${tableName}
    <set>
<#if columns??>
  <#list columns as column>
    <#if column.columnType != 'VARCHAR' && column.columnType != 'CHAR' && column.delete = 'Y'>
    <#if (column.defaultVal = '') >
  	<if  test="${column.changeField} != null">
         ${column.column} = ${r"#{"} ${column.changeField},jdbcType=${column.columnType}${r"}"},
    </if>
    <#else>
    ${column.column} = ${column.defaultVal},
  	</#if>
  	</#if>
  	<#if (column.columnType = 'VARCHAR' || column.columnType = 'CHAR') && column.delete = 'Y'>
    <#if (column.defaultVal = '') >
  	<if test="${column.changeField} != null  and ${column.changeField} != '' ">
         ${column.column} = ${r"#{"} ${column.changeField},jdbcType=${column.columnType}${r"}"},
    </if>
    <#else>
    ${column.column} = ${column.defaultVal},
  	</#if>
  	</#if>
  </#list>
</#if> 
    </set>
    where id = ${r"#{"}id,jdbcType=BIGINT${r"}"}
  </update>
</mapper>